from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction, QMenu
from PyQt5.QtWidgets import QSystemTrayIcon


class SimpleTrayIcon(QSystemTrayIcon):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.menu = QMenu()
        self.setContextMenu(self.menu)
        self.activated.connect(self.__slot_onActivated)
        self.show()

    def setIcon(self, path):
        super().setIcon(QIcon(path))

    def addMenuLine(self, text, handler):
        newAction = QAction(self.menu)
        newAction.setText(text)
        newAction.triggered.connect(handler)
        self.menu.addAction(newAction)

    def setTip(self, text):
        self.setToolTip(text)

    def __slot_onActivated(self, reason):
        if reason == QSystemTrayIcon.Trigger:
            self.__showParent()
        if reason == QSystemTrayIcon.DoubleClick:
            self.__showParent()

    def __showParent(self):
        self.parent().showNormal()
        self.parent().raise_()
        self.parent().activateWindow()
